#include "stdafx.h"
#include "RTFramework.h"
#include "RTWindowApplication.h"
#include "RTRenderer.h"
#include "RTCamera.h"
#include "RTTimer.h"
#include "RTMacro.h"

RTFramework::RTFramework()
:	m_pRTApp( NULL ),
	m_pRTRenderer( NULL ),
	m_pRTTimer( NULL ),
	m_dwClearColor( D3DCOLOR_ARGB( 1, 0, 0, 0 ) )
{
	m_pRTApp = new RTWindowApplication;
	m_pRTRenderer = new RTRenderer;
	m_pRTCamera = new RTCamera;
	m_pRTTimer = new RTTimer;
}

RTFramework::~RTFramework()
{
	Release();
}

BOOL	RTFramework::Initialize( TCHAR* pszWindowCaption, UINT nWindowWidth, UINT nWindowHeight, BOOL bWindowed, DWORD dwWindowStyle )
{
	if( !m_pRTApp )	return FALSE;

	m_pRTApp->Initialize( pszWindowCaption, nWindowWidth, nWindowHeight, bWindowed, dwWindowStyle );

	return TRUE;
}

BOOL	RTFramework::CreateRenderer()
{
	BOOL bResult = TRUE;
	if( m_pRTRenderer && m_pRTApp )
	{
		bResult = m_pRTRenderer->Create( m_pRTApp->GetWidth(), m_pRTApp->GetHeight(), m_pRTApp->IsWindowedMode(), m_pRTApp->GetHWND() );
		m_pRTCamera->Create( m_pRTRenderer->GetDevice() );
	}

	return bResult;
}

void	RTFramework::Run()
{
	if( !m_pRTApp || !m_pRTRenderer || !m_pRTTimer )
		return ;

	m_pRTTimer->Update();

	Update( m_pRTTimer->GetElapsedTime() );

	m_pRTRenderer->Begin_Render( m_dwClearColor );
	Render( m_pRTTimer->GetElapsedTime() );
	m_pRTRenderer->End_Render();
}

void	RTFramework::Update( const double elapsedTime )
{
	m_pRTCamera->Update( m_pRTRenderer->GetDevice() );
}

void	RTFramework::Render( const double elapsedTime )
{

}

void	RTFramework::Restore()
{

}

void	RTFramework::Release()
{
	RTSAFE_DELETE( m_pRTTimer );
	RTSAFE_DELETE( m_pRTCamera );
	RTSAFE_DELETE( m_pRTRenderer );
	RTSAFE_DELETE( m_pRTApp );
}

RTWindowApplication*	RTFramework::GetRTWinApp()
{
	return m_pRTApp;
}

void	RTFramework::SetClearColor( DWORD dwColor )
{
	m_dwClearColor = dwColor;
}

LRESULT CALLBACK	RTFramework::WinProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
	switch( msg )
	{
	case WM_DESTROY :
		{
			PostQuitMessage( 0 );
			break;
		}
	case WM_CLOSE :
		{
			DestroyWindow( hWnd );
			break;
		}
	};

	return DefWindowProc( hWnd, msg, wParam, lParam );
}